package com.thh.article.service.impl;

import com.thh.article.service.AppArticleService;
import com.thh.common.constans.ArticleConstans;
import com.thh.model.article.dtos.ArticleHomeDto;
import com.thh.model.article.pojos.ApArticle;
import com.thh.model.common.dtos.ResponseResult;
import com.thh.model.mappers.app.ApArticleMapper;
import com.thh.model.mappers.app.ApUserArticleListMapper;
import com.thh.model.user.pojos.ApUser;
import com.thh.model.user.pojos.ApUserArticleList;
import com.thh.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AppArticleServiceImpl implements AppArticleService {

    private static final short MAX_PAGE_SIZE = 50;


    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
      //参数校验
        if (dto ==null){
            dto= new ArticleHomeDto();
        }
        //时间校验
        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        //分页参数的校验
        Integer size = dto.getSize();
        if (size ==null ||size<=0)
        {
            size = 20;
        }
        Math.min(size,MAX_PAGE_SIZE);//如果size大于50  取值50   不然就取size
        dto.setSize(size);

        //文章频道参数
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstans.DEFAULT_TAG);//查询所有
        }

        //类型参数校验
        if (!type.equals(ArticleConstans.LOADTYPE_LOAD_MORE)&&!type.equals(ArticleConstans.LOADTYPE_LOAD_NEW)){
            type =     ArticleConstans.LOADTYPE_LOAD_MORE;
        }

        //用户校验
        ApUser user = AppThreadLocalUtils.getUser();



        //获取用户的信息
        if (user!=null){//判断用户是否存在
            //存在 已登录  加载推荐文章
         List<ApArticle> apArticleList =     getUserArticle(user,dto,type);
        return ResponseResult.okResult(apArticleList);
        }else {
            //不存在  未登录 加载默认文章
            List<ApArticle> apArticleList =     getDefaultArticle(dto,type);
            return ResponseResult.okResult(apArticleList);
        }


    }

    @Autowired
    private ApUserArticleListMapper apUserArticleListMapper;
    /**
     * 查找默认的文章信息
     * @param dto
     * @param type
     * @return
     */
    private List<ApArticle> getDefaultArticle(ArticleHomeDto dto, Short type) {

        return apArticleMapper.loadArticleListByLocation(dto,type);
    }



    /**
     * 先从用户查找文章信息，如果没有就查找默认的文章
     * @param user
     * @param dto
     * @param type
     * @return
     */
    private List<ApArticle> getUserArticle(ApUser user, ArticleHomeDto dto, Short type) {
       List<ApUserArticleList> list = apUserArticleListMapper.loadArticleListByUser(user,dto,type);
       if (!list.isEmpty()){
           return apArticleMapper.loadArticleListByIdList(list);
       }else {
           return apArticleMapper.loadArticleListByLocation(dto, type);
       }
    }
}
